#!/bin/bash
#sh uploadArgSetForEnv_pgyer.sh -pagyerDownJsonF "${Pgyer_Download_FILE_PATH}" -envJsonF "${ENVIRONMENT_FILE_PATH}"
#sh uploadArgSetForEnv_pgyer.sh -pagyerDownJsonF "../example_packing_info/app_info.json" -envJsonF "../example_packing_info/app_info.json"

echo "===========进入脚本$0==========="

exit_script() { # 退出脚本的方法，省去当某个步骤失败后，还去继续多余的执行其他操作
    exit 1
}


JQ_EXEC=`which jq`

# shell 参数具名化
show_usage="args: [-pagyerDownJsonF, -envJsonF]\
                                  [--pgyer-download-json-file=, --environment-json-file=]"

while [ -n "$1" ]
do
        case "$1" in
                -pagyerDownJsonF|--pgyer-download-json-file) Pgyer_Download_FILE_PATH=$2; shift 2;;
                -envJsonF|-environment-json-file) ENVIRONMENT_FILE_PATH=$2; shift 2;;
                --) break ;;
                *) echo $1,$2,$show_usage; break ;;
        esac
done

# 当前【shell脚本】的工作目录
# $PWD代表获取当前路径，当cd后，$PWD也会跟着更新到新的cd路径。这个和在终端操作是一样的道理的
CurrentDIR_Script_Absolute="$( cd "$( dirname "$0" )" && pwd )"
#echo "CurrentDIR_Script_Absolute=${CurrentDIR_Script_Absolute}"
#CommonFun_HomeDir_Absolute=${CurrentDIR_Script_Absolute}/..
CommonFun_HomeDir_Absolute=${CurrentDIR_Script_Absolute%/*} # 使用此方法可以避免路径上有..

qscript_path_get_filepath="${CommonFun_HomeDir_Absolute}/qscript_path_get.sh"
qbase_update_json_file_singleString_script_path="$(sh ${qscript_path_get_filepath} qbase update_json_file_singleString)"


if [ ! -f "${CommonFun_HomeDir_Absolute}/a_function.sh" ]; then
    PackageErrorCode=1
    PackageErrorMessage="引入方法文件:《 source ${CommonFun_HomeDir_Absolute}/a_function.sh ${CommonFun_HomeDir_Absolute} 》失败，请检查"
    updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
    exit_script
fi
source ${CommonFun_HomeDir_Absolute}/a_function.sh ${CommonFun_HomeDir_Absolute}

if [ -z "${Pgyer_Download_FILE_PATH}" ]; then
    PackageErrorCode=1
    PackageErrorMessage="执行本脚本需要传入 -pagyerDownJsonF 参数，请检查您的执行命令！"
    updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
    exit_script
elif [ ! -f "${Pgyer_Download_FILE_PATH}" ];then
    PackageErrorCode=1
    PackageErrorMessage="您的Pgyer_Download_FILE_PATH=${Pgyer_Download_FILE_PATH}文件不存在，请检查！"
    updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
    exit_script
fi

if [ -z "${ENVIRONMENT_FILE_PATH}" ]; then
    PackageErrorCode=1
    PackageErrorMessage="执行本脚本需要传入 -envJsonF 参数，请检查您的执行命令！"
    updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
    exit_script
elif [ ! -f "${ENVIRONMENT_FILE_PATH}" ];then
    PackageErrorCode=1
    PackageErrorMessage="您的ENVIRONMENT_FILE_PATH=${ENVIRONMENT_FILE_PATH}文件不存在，请检查！"
    updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
    exit_script
fi


source ${CommonFun_HomeDir_Absolute}/a_source_env.sh -appInfoF "${ENVIRONMENT_FILE_PATH}" # 获取 BRANCH\PlatformType\PackageTargetType\PackageNetworkType

ShortBranceName=${BRANCH##*/}
echo "ShortBranceName=$ShortBranceName"
echo "------PlatformType:${PlatformType}"
echo "------PackageTargetType:${PackageTargetType}"
echo "------PackageNetworkType:${PackageNetworkType}"


pgyerParamsRootMapString=$(cat ${Pgyer_Download_FILE_PATH} | ${JQ_EXEC} -r ".package_pgyer_params")
if [ "${pgyerParamsRootMapString}" == "null" ] || [ -z "${pgyerParamsRootMapString}" ]; then
    PackageErrorCode=1
    PackageErrorMessage="你的${Pgyer_Download_FILE_PATH}中未添加package_pgyer_params参数，会导致即使打包成功，蒲公英也无法上传成功"
    updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
    exit_script # 这是严重错误，所以直接退出本脚本，没必要继续执行
fi



# 获取指定环境的所有打包依据
function get_network_PgyerRootMapString() {
    # 是map时候
    #network_PgyerRootMapString=$(echo ${pgyerParamsRootMapString} | ${JQ_EXEC} -r --arg networkKey "$networkKey" '.[].$networkKey')

    # 是数组时候
    network_PgyerRootMapString=$(echo ${pgyerParamsRootMapString} | ${JQ_EXEC} -r ".[] | select(.network==\"${PackageNetworkType}\")")
    # echo "network_PgyerRootMapString=${network_PgyerRootMapString}"
    if [ "${network_PgyerRootMapString}" == "null" ] || [ -z "${network_PgyerRootMapString}" ]; then
        PackageErrorCode=1
        PackageErrorMessage="获取 ${Pgyer_Download_FILE_PATH} 文件中 package_pgyer_params  里环境为 ${PackageNetworkType} 环境的值失败，请检查！"
        updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
        exit_script # 这是严重错误，所以直接退出本脚本，没必要继续执行
    fi

    sh ${CommonFun_HomeDir_Absolute}/update_json_file.sh -f "${Pgyer_Download_FILE_PATH}" -k "package_pgyer_params_current" -v "${network_PgyerRootMapString}"
    if [ $? != 0 ]; then
        # echo "❌Error：执行命令失败(更新package_pgyer_params_current)：《 sh ${CommonFun_HomeDir_Absolute}/update_json_file.sh -f \"${Pgyer_Download_FILE_PATH}\" -k \"package_pgyer_params_current\" -v \"${network_PgyerRootMapString}\" 》"
        PackageErrorCode=1
        PackageErrorMessage="更新 package_pgyer_params_current 属性失败，请检查！"
        updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
        exit_script "更新 package_pgyer_params_current 属性失败，请检查！"
    fi
}

exitWithErrorMessage() {
    PackageErrorCode=1
    PackageErrorMessage=$1
    updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
    exit_script
}

# 遍历所有允许打包的依据，检查指定分支是否可打包及其依据
function checkAndGetAllowBasis() {
    networkKey=$1   #'preproduct'
    branchKey=$2    #'dev_publish_in'
    platformKey=$3  #'iOS'

    network_allowBranchConfig_String=$(echo "${network_PgyerRootMapString}" | ${JQ_EXEC} -r ".allowBranchConfig[]")
    if [ -z "${network_allowBranchConfig_String}" ] || [ "${network_allowBranchConfig_String}" == "null" ]; then
        echo "执行命令失败(获取所有允许打包的依据):《 echo \"${network_PgyerRootMapString}\" | ${JQ_EXEC} -r \".allowBranchConfig[]\" 》"
        return 1
    fi
    network_allowBranchConfig_Count=$(echo "${network_PgyerRootMapString}" | ${JQ_EXEC} -r ".allowBranchConfig|length")
    # echo "network_allowBranchConfig_Count=${network_allowBranchConfig_Count},\n network_allowBranchConfig_String=${network_allowBranchConfig_String}"
    
    hasFoundBranchConfig='false'
    targetAllowBranchConfig_String=""
    for ((i=0;i<network_allowBranchConfig_Count;i++))
    do
        iAllowBranchConfig_String=$(echo "${network_PgyerRootMapString}" | ${JQ_EXEC} -r ".allowBranchConfig[$i]")
        #echo "$((i+1)) iAllowBranchConfig_String=${iAllowBranchConfig_String}"

        # 1、有配置正则的时候，先判断是否符合正则
        allowBranchRegularsString=$(echo "${iAllowBranchConfig_String}" | ${JQ_EXEC} -r ".branchRegulars")
        if [ -n "${allowBranchRegularsString}" ] && [ "${allowBranchRegularsString}" != "null" ]; then
            echo "${PackageNetworkType}环境支持使用符合以下正则的分支来打包，正则内容如下：${allowBranchRegularsString}"
            allowBranchsRegularCount=$(echo "${iAllowBranchConfig_String}" | ${JQ_EXEC} -r ".branchRegulars|length")
            #echo "$((i+1)) allowBranchsNameCount=${allowBranchsNameCount}, allowBranchNamesString=${allowBranchNamesString}"
            for ((j=0;j<allowBranchsRegularCount;j++))
            do
                allowBranchRegular=$(echo "${allowBranchRegularsString}" | ${JQ_EXEC} -r ".[$j]")
                #echo "$((i+1)).$((j+1)) allowBranchRegular=${allowBranchRegular}, branchKey=${branchKey}"
                # if echo "${allowBranchNamesString[@]}" | grep -wq "*" &>/dev/null; then
                if echo "$branchKey" | grep -qE "${allowBranchRegular}"; then
                    targetAllowBranchConfig_String=$iAllowBranchConfig_String
                    hasFoundBranchConfig='true'
                    break
                fi
            done

            if [ "${hasFoundBranchConfig}" == 'true' ]; then
                echo "恭喜:${PackageNetworkType}环境支持使用${branchKey}分支来打包(附其判断依据为:${targetAllowBranchConfig_String})"
                break
            fi
        fi

        # 2、没有配置正则内容的时候，或者匹配不上的时候，判断是否符合具体分支
        allowBranchNamesString=$(echo "${iAllowBranchConfig_String}" | ${JQ_EXEC} -r ".branchNames")
        if [ -n "${allowBranchNamesString}" ] && [ "${allowBranchNamesString}" != "null" ]; then
            allowBranchsNameCount=$(echo "${iAllowBranchConfig_String}" | ${JQ_EXEC} -r ".branchNames|length")
            #echo "$((i+1)) allowBranchsNameCount=${allowBranchsNameCount}, allowBranchNamesString=${allowBranchNamesString}"
            for ((j=0;j<allowBranchsNameCount;j++))
            do
                allowBranchName=$(echo "${allowBranchNamesString}" | ${JQ_EXEC} -r ".[$j]")
                #echo "$((i+1)).$((j+1)) allowBranchName=${allowBranchName}, branchKey=${branchKey}"
                if [ "${allowBranchName}" == ${branchKey} ]; then
                    echo "---------------------------------"
                    targetAllowBranchConfig_String=$iAllowBranchConfig_String
                    hasFoundBranchConfig='true'
                    break
                fi
            done

            if [ "${hasFoundBranchConfig}" == 'true' ]; then
                echo "恭喜:${PackageNetworkType}环境支持使用${branchKey}分支来打包(附其判断依据为:${targetAllowBranchConfig_String})"
                break
            fi
        fi
    done

    if [ "${hasFoundBranchConfig}" != 'true' ]; then
        return 1
    fi
}


# 获取指定环境的所有打包依据
get_network_PgyerRootMapString
if [ $? != 0 ]; then
    exit_script
fi

# 遍历所有允许打包的依据，检查指定分支是否可打包及其依据
checkAndGetAllowBasis "${PackageNetworkType}" "${ShortBranceName}" "${PlatformType}"
if [ $? != 0 ]; then
    echo "失败:${PackageNetworkType}环境不支持使用${branchKey}分支来打包"
    exit_script
fi
echo "恭喜:${PackageNetworkType}环境支持使用${branchKey}分支来打包targetAllowBranchConfig_String=${targetAllowBranchConfig_String}"
echo "原本依据：${network_PgyerRootMapString}"




function updatePgyerMainInfo(){
    networkKey=$1   #'preproduct'
    branchKey=$2    #'dev_publish_in'
    platformKey=$3  #'iOS'
    
    network_pgyer_owner=$(echo "${network_PgyerRootMapString}" | ${JQ_EXEC} -r ".owner")
    network_pgyer_pgyerKey=$(echo "${network_PgyerRootMapString}" | ${JQ_EXEC} -r ".pgyerKey")
    
    network_platform_pgyer_Map=$(echo "${network_PgyerRootMapString}" | ${JQ_EXEC} -r --arg platformKey "$platformKey" '.[$platformKey]')
    network_pgyer_appOfficialWebsite=$(echo "${network_platform_pgyer_Map}" | ${JQ_EXEC} -r ".appOfficialWebsite")
    
    echo "network_pgyer_owner=${network_pgyer_owner}"
    if [ "${network_pgyer_owner}" != "null" ] && [ -n "${network_pgyer_owner}" ]; then
        sh ${qbase_update_json_file_singleString_script_path} -jsonF ${Pgyer_Download_FILE_PATH} -k 'package_result.shoudUploadToPgyerOwner' -v "${network_pgyer_owner}"
        if [ $? != 0 ]; then
            return 1
        fi
    fi
    
    echo "network_pgyer_pgyerKey=${network_pgyer_pgyerKey}"
    if [ "${network_pgyer_pgyerKey}" != "null" ] && [ -n "${network_pgyer_pgyerKey}" ]; then
        sh ${qbase_update_json_file_singleString_script_path} -jsonF ${Pgyer_Download_FILE_PATH} -k 'package_result.shoudUploadToPgyerKey' -v "${network_pgyer_pgyerKey}"
        if [ $? != 0 ]; then
            return 1
        fi
    fi

    echo "network_pgyer_appOfficialWebsite=${network_pgyer_appOfficialWebsite}" # 网址有斜杠，所以使用sed_text.sh中的方法，其已帮处理斜杠问题
    if [ "${network_pgyer_appOfficialWebsite}" != "null" ] && [ -n "${network_pgyer_appOfficialWebsite}" ]; then
        sh ${qbase_update_json_file_singleString_script_path} -jsonF ${Pgyer_Download_FILE_PATH} -k 'package_result.package_official_website' -v "${network_pgyer_appOfficialWebsite}"
        if [ $? != 0 ]; then
            return 1
        fi
    fi
}

PgyerUploadUrlType=$(cat ${Pgyer_Download_FILE_PATH} | ${JQ_EXEC} -r ".package_result.pgyerUploadUrlType")
if [ $? != 0 ] || [ "${PgyerUploadUrlType}" == "null" ]; then
    PackageErrorCode=1
    PackageErrorMessage="获取.package_result.pgyerUploadUrlType 失败：《 cat ${Pgyer_Download_FILE_PATH} | ${JQ_EXEC} -r \".package_result.pgyerUploadUrlType\" 》，请检查"
    updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
    exit_script # 这是严重错误，所以直接退出本脚本，没必要继续执行
fi
echo "------PgyerUploadUrlType:${PgyerUploadUrlType}"

function getPgyerChannel(){
    networkKey=$1   #'preproduct'
    branchKey=$2    #'dev_publish_in'
    platformKey=$3  #'iOS'
    PgyerUploadUrlType=$4 # 是否直接上传到下载的渠道地址(也可以理解为说不上传到临时地址)
    
    targetBranchConfig_mayString=$(echo "${targetAllowBranchConfig_String}" | ${JQ_EXEC} -r ".branchChannelConfig")
    #echo "targetBranchConfig_mayString=${targetBranchConfig_mayString}"
    echo "=================${branchKey}分支的蒲公英匹配参数为targetBranchConfig_mayString=${targetBranchConfig_mayString}"
    if [ -z "${targetBranchConfig_mayString}" ] || [ "${targetBranchConfig_mayString}" == "null" ]; then
        PackageErrorCode=-1
        PackageErrorMessage="允许打蒲公英${PackageNetworkType}环境的包，未包括${branchKey}分支，故无法找到本包上传蒲公英时候的匹配参数，请检查【${Pgyer_Download_FILE_PATH}】文件中的package_pgyer_params参数"
        updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
        return 1
    fi
    
    echo "正在执行命令《getChannelValueFromPgyerBranchConfigWithPlatform \"${targetBranchConfig_mayString}\" \"${PlatformType}\"》"
    source a_function_result.sh
    getChannelValueFromPgyerBranchConfigWithPlatform "${targetBranchConfig_mayString}" "${platformKey}"
    if [ -z "${packagePgyerChannelShortcutResult_upload}" ] || [ "${packagePgyerChannelShortcutResult_upload}" == "null" ]; then
        PackageErrorCode=1
        PackageErrorMessage="请在${Pgyer_Download_FILE_PATH}为${PackageNetworkType}环境的${ShortBranceName}分支创建${PlatformType}平台的渠道号"
        updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
        exit_script # 这是严重错误，所以直接退出本脚本，没必要继续执行
    fi

    if [ -z "${packagePgyerChannelShortcutResult_download}" ] || [ "${packagePgyerChannelShortcutResult_download}" == "null" ]; then
        echo "友情提示🤝:下载地址缺失，所以非指定情况下，下载地址 即为 上传地址"
        packagePgyerChannelShortcutResult_download=${packagePgyerChannelShortcutResult_upload}
        packagePgyerChannelKeyResult_download=${packagePgyerChannelKeyResult_upload}
    fi
    # 是否直接上传到下载的渠道地址(也可以理解为说不上传到临时地址)
    if [ "$PgyerUploadUrlType" == "toDownUrl" ]; then
        echo "友情提示🤝：想直接上传到下载地址，所以上传地址为最终的下载地址"
        packagePgyerChannelShortcutResult_upload=${packagePgyerChannelShortcutResult_download}
        packagePgyerChannelKeyResult_upload=${packagePgyerChannelKeyResult_download}
    fi
    
    # 需事先在蒲公英上建立此渠道短链，否则会提示The channel shortcut URL is invalid
    echo "上传目标：只会上传到蒲公英的上的【指定渠道】:${packagePgyerChannelShortcutResult_upload}"
    
    
    # [Mac环境下shell脚本中的map](https://www.jianshu.com/p/a55480b793b0)
#    declare -A myMap=(["my00"]="00" ["my01"]="01")
#    myMap["my02"]="02"
#    myMap["my03"]="03"
#    declare -A targetBranchConfig_realMap=()
#    targetBranchConfig_realMap["uploadChannelShortcut"]="${packagePgyerChannelShortcutResult_upload}"
#    targetBranchConfig_realMap["uploadChannelKey"]="${packagePgyerChannelKeyResult_upload}"
    
    uploadChannelShortcut_key="uploadChannelShortcut"
    uploadChannelKey_key="uploadChannelKey"
    targetBranchConfig_realMap="{"
    targetBranchConfig_realMap+="\"${uploadChannelShortcut_key}\":\"${packagePgyerChannelShortcutResult_upload}\", \"${uploadChannelKey_key}\":\"${packagePgyerChannelKeyResult_upload}\""
    if [ -z "${packagePgyerChannelShortcutResult_download}" ] || [ "${packagePgyerChannelShortcutResult_download}" == "null" ]; then
        packagePgyerChannelShortcutResult_download=${packagePgyerChannelShortcutResult_upload}
        packagePgyerChannelKeyResult_download=${packagePgyerChannelKeyResult_upload}
    fi
    targetBranchConfig_realMap+=","
    targetBranchConfig_realMap+="\"downloadChannelShortcut\":\"${packagePgyerChannelShortcutResult_download}\", \"downloadChannelKey\":\"${packagePgyerChannelKeyResult_download}\""
    targetBranchConfig_realMap+="}"
    echo "====================targetBranchConfig_realMap=${targetBranchConfig_realMap}"
    
    echo "正在执行命令(更新package_result.pgyer_branch_config)：《sh ${CommonFun_HomeDir_Absolute}/update_json_file.sh -f \"${Pgyer_Download_FILE_PATH}\" -k \"package_result.pgyer_branch_config\" -v \"${targetBranchConfig_realMap}\"》"
    sh ${CommonFun_HomeDir_Absolute}/update_json_file.sh -f "${Pgyer_Download_FILE_PATH}" -k "package_result.pgyer_branch_config" -v "${targetBranchConfig_realMap}"
}

#function updatePgyerBranchConfig() {
#    sh ${CommonFun_HomeDir_Absolute}/update_json_file.sh -f "${Pgyer_Download_FILE_PATH}" -k "package_result.pgyer_branch_config" -v "${targetBranchConfig_realMap}"
#}

function updatePgyerSubInfo_ChannelKey() {
    echo "正在执行命令:《 getPgyerChannel \"${PackageNetworkType}\" \"${ShortBranceName}\" \"${PlatformType}\"》"
    getPgyerChannel "${PackageNetworkType}" "${ShortBranceName}" "${PlatformType}" "${PgyerUploadUrlType}"
    return $?
    
    OnlyToCURCHANNEL=$1 # "yes" or "no"
    APPENVIRONMENT=$2
    echo "OnlyToCURCHANNEL=${OnlyToCURCHANNEL},APPENVIRONMENT=${APPENVIRONMENT}"
    
    
    if [ $OnlyToCURCHANNEL != 'yes' ] # 当串的长度大于0时为真(串非空):是否只上传到当前渠道
    then
        echo "上传目标：会上传到蒲公英的上的【所有渠道】"
        sh ${qbase_update_json_file_singleString_script_path} -jsonF ${Pgyer_Download_FILE_PATH} -k 'package_result.shoudUploadToPgyerChannelShortcut' -v "upload to all pgyer channelShortcut"
        return 1 # 注意：这里不能用exit，否则本脚本该方法后的其他代码会无法执行
    fi
    
    APPENVIRONMENT_lower=`echo $APPENVIRONMENT | tr '[:upper:]' '[:lower:]'` # 将大小转小写
    #echo "APPENVIRONMENT_lower=$APPENVIRONMENT_lower"
    
    # APPENVIRONMENT_ForChannel 是为了避免最后的名字channel太长
    if [ ${APPENVIRONMENT_lower} == "product" ];      # 生产环境
    then
        APPENVIRONMENT_ForChannel="pro"
    elif [ ${APPENVIRONMENT_lower} == "preproduct" ]; # 预生产环境
    then
        APPENVIRONMENT_ForChannel="pre"
    elif [ ${APPENVIRONMENT_lower} == "develop1" ];   # 测试环境1
    then
        APPENVIRONMENT_ForChannel="dev1"
    elif [ ${APPENVIRONMENT_lower} == "develop2" ];   # 测试环境2
    then
        APPENVIRONMENT_ForChannel="dev2"
    elif [ ${APPENVIRONMENT_lower} == "develop3" ];   # 测试环境3
    then
        APPENVIRONMENT_ForChannel="dev3"
    else                                        # 不是允许/支持的环境
        echo "-------- Error：脚本${0}执行失败。\
    所执行的脚本命令中，APPENVIRONMENT=${APPENVIRONMENT},转换为小写后的值APPENVIRONMENT_lower=${APPENVIRONMENT_lower},\
    不是product,preproduct,develop1,develop2,develop3中的一种(严格区分大小写)，\
    故不再继续执行 --------"
        exit_script
    fi

    # 区分平台
    PlatformTypeFlag=`echo ${PlatformType:0:1}` # 获取字符串的第一个字符
    
    if [ -n "${ShortBranceName}" ] # 当串的长度大于0时为真(串非空)
    then
        DOT="."
        if [[ "${ShortBranceName}" == *$DOT* ]]
        then
            echo "分支${ShortBranceName}包含.，所以其不能作为渠道，下面会自动替换其中的.为_"
            #替换分支名中的.为_
            #sed -i '' "s/./_/g" $Channel_FEATUREBRANCH_File # 文件时候
            Channel_FEATUREBRANCH=${ShortBranceName//./_} # 字符串里的直接修改
        else
            #echo "分支${ShortBranceName}不包含.，所以其可直接作为渠道"
            Channel_FEATUREBRANCH=$ShortBranceName
        fi

        buildChannelShortcut="${APPENVIRONMENT_ForChannel}-${Channel_FEATUREBRANCH}-${PlatformTypeFlag}"
    else
        buildChannelShortcut="${APPENVIRONMENT_ForChannel}-${PlatformTypeFlag}"
    fi
    
    
    
    
    # 需事先在蒲公英上建立此渠道短链，否则会提示The channel shortcut URL is invalid
    echo "上传目标：只会上传到蒲公英的上的【指定渠道】:${buildChannelShortcut}"
    sh ${qbase_update_json_file_singleString_script_path} -jsonF ${Pgyer_Download_FILE_PATH} -k 'package_result.shoudUploadToPgyerChannelShortcut' -v "${buildChannelShortcut}"
}




updatePgyerMainInfo "${PackageNetworkType}" "${ShortBranceName}" "${PlatformType}"
if [ "${PackageNetworkType}" == "develop1" -o "${PackageNetworkType}" == 'develop2' ] ; then
#    echo "暂都上传都所有渠道"
    if [ $? != 0 ]; then
        PackageErrorCode=1
        PackageErrorMessage="${PackageNetworkType}环境的蒲公英的渠道获取错误，请检查！"
        updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
        exit_script
    fi
    
elif [ "${PackageNetworkType}" == "test1" ] || [ "${PackageNetworkType}" == "test2" ] ; then
    updatePgyerSubInfo_ChannelKey
    if [ $? != 0 ]; then
        PackageErrorCode=1
        PackageErrorMessage="${PackageNetworkType}环境的蒲公英的渠道获取错误，请检查！"
        updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
        exit_script
    fi

elif [ "${PackageNetworkType}" == "preproduct" ] ; then
    updatePgyerSubInfo_ChannelKey
    if [ $? != 0 ]; then
        PackageErrorCode=1
        PackageErrorMessage="${PackageNetworkType}环境的蒲公英的渠道获取错误，请检查！"
        updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
        exit_script
    fi
    
elif [ "${PackageNetworkType}" == 'product' ] ; then
    updatePgyerSubInfo_ChannelKey
    if [ $? != 0 ]; then
        PackageErrorCode=1
        PackageErrorMessage="${PackageNetworkType}环境的蒲公英的渠道获取错误，请检查！"
        updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
        exit_script
    fi
    
else
    PackageErrorCode=1
    PackageErrorMessage="上传蒲公英，环境变量不对，您的脚本里没有针对${PackageNetworkType}环境的判断，从而无法找到提供给该环境的的PYGERKEY，请检查！"
    updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
    exit_script
fi
